# -*- coding:utf-8 -*-
import numpy as np
import random

from preprocess_data import get_data

class one_variable(object):
	"""docstring for one_variable"""
	def __init__(self):
		super(one_variable, self).__init__()
		features,labels = get_data('训练集')
		
		# 单变量搜索法
		# 这里G为标准化后每个类均值标准差

		# step 1. 按照标签顺序排好
		sorted_nums = sorted(enumerate(labels.tolist()), key=lambda x: x[1])
		single_varible_search_features = np.array([features[i[0]] for i in sorted_nums])
		single_varible_search_labels = np.array([labels[i[0]] for i in sorted_nums])

		# step 2. 对特征进行标准化处理（为了普适性，虽然一开始进行了这个操作，此时均值为0，方差为1）
		single_varible_search_features_mean = single_varible_search_features.mean(axis = 0,keepdims = True)
		single_varible_search_features_std = single_varible_search_features.std(axis = 0,keepdims = True)
		single_varible_search_features = (single_varible_search_features - single_varible_search_features_mean)/\
									     (single_varible_search_features_std+1e-8) # 利用广播机制

		# step 3. 计算类间标准差
		single_varible_search_features = single_varible_search_features.reshape(200,144,440).mean(axis = 1).std(axis = 0)/\
										 single_varible_search_features.reshape(200,144,440).std(axis = 1).sum(axis = 0)
		sort_features = sorted(enumerate(single_varible_search_features.tolist()), key=lambda x: x[1],reverse=True)
		# print(sort_features) # 已经按照特征重要性排好序
		self.feature = [f[0] for f in sort_features]
	
	def get_feature(self,n_feature=440):
		n_feature = min(n_feature,len(self.feature))
		return self.feature[:n_feature]

